package controller;

import com.alibaba.fastjson.JSON;
import com.xiaoming.coupon.exception.CouponException;
import com.xiaoming.coupon.vo.SettlementInfo;
import executor.ExecuteManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author jxbai
 * @desc 结算服务 controller
 * @date 2020/7/3
 */
@Slf4j
@Controller
public class SettlementController {

    /**
     * 计算规则执行管理器
     */
    private final ExecuteManager executeManager;

    @Autowired
    public SettlementController(ExecuteManager executeManager) {
        this.executeManager = executeManager;
    }

    /**
     * 优惠券结算
     * @param settlement
     * @return
     * @throws CouponException
     */
    @PostMapping("/settlement/compute")
    public SettlementInfo conputeRule(@RequestBody SettlementInfo settlement) throws CouponException {
        log.info("settlement: {}", JSON.toJSONString(settlement));
        return executeManager.computeRule(settlement);
    }
}
